<?php

//error_reporting(E_ALL);
//ini_set('display_errors','On');

require_once 'Zend/Controller/Action/Helper/FlashMessenger.php';

class Admin_ConfiguracionController extends Zend_Controller_Action {

    private $_formulaDao;
    private $_flashMessenger;

    public function init() {
        $auth = Zend_Auth::getInstance();

        if ($auth->hasIdentity()) {

            if (!empty($auth->getIdentity()->id)) {
                $usuarioDao = new Doctrine_Model_UsuariosDoctrineDao();
                $this->view->usuario = $usuarioDao->obtenerPorId($auth->getIdentity()->id);
            }
        } else {
            $this->_helper->redirector('index', 'login', 'admin');
        }
        $this->_helper->layout()->setLayout("layout_admin");

        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->_flashMessenger->clearMessages();
        $this->_sucursalDao = new Doctrine_Model_SucursalDao();
        $this->_formulaDao = new Doctrine_Model_FormulaDoctrineDao();
    }

    public function indexAction() {
        
    }

    public function tasaAction() {
        $formula = new Doctrine_Model_Entities_Formula();
        if ($this->getRequest()->isPost()) {
            if ($this->_hasParam("form_tasa")) :
                $formula_id = $this->getRequest()->getParam('formula_id');
                $formula = $this->_formulaDao->obtenerPorId($formula_id);
                $tasas = $formula->getTasas();
                foreach ($tasas as $tasa) :
                    if ($tasa->getSucursal()->getId() == $this->view->usuario->getSucursal()->getId()) {
                        $tasa->setActivo(0);
                        $formula->agregarTasa($tasa);
                    }
                endforeach;
                for ($i = 1; $i < 13; $i++) :
                    if ($this->getRequest()->getParam("tasa_$i")) :
                        $tasa = new Doctrine_Model_Entities_Tasa();
                        $tasa->setValor($this->getRequest()->getParam("tasa_$i"));
                        $tasa->setDesde($this->getRequest()->getParam("cuota_desde_$i"));
                        $tasa->setHasta($this->getRequest()->getParam("cuota_hasta_$i"));
                        $tasa->setFormula($formula);
                        $tasa->setFecha(new DateTime());
                        $tasa->setActivo(1);
                        $tasa->setSucursal($this->view->usuario->getSucursal());
                        $formula->agregarTasa($tasa);
                    endif;
                endfor;
                if ($this->_formulaDao->guardar($formula)) :
                    $this->_flashMessenger->addMessage('Formula guardada correctamente.');
                endif;
            elseif ($this->_hasParam("formula")) :
                $formula_id = $this->getRequest()->getParam('formula');
            endif;
        }

        $formula = $this->_formulaDao->obtenerPorId($formula_id);

        if ($formula) :
            $this->view->titulo = $formula->getDescripcion();
            $this->view->formula = $formula;
        endif;

        $this->view->messages = $this->_helper->flashMessenger->getCurrentMessages();

        $this->view->formula_id = $formula_id;
        $this->view->formulas = $this->_formulaDao->obtenerTodosNoNegocios();
    }

    public function tasanegocioAction() {
        $formula = new Doctrine_Model_Entities_Formula();
        $formula_id = $this->getRequest()->getParam('formula_id');
        if ($this->getRequest()->isPost()) {
            $a=$this->getRequest()->getParam('soloActualizaValor');
            if(!$this->getRequest()->getParam('soloActualizaValor')) {
                if ($this->_hasParam("form_tasa")) :

                    $formula = $this->_formulaDao->obtenerPorId($formula_id);
                    $tasas = $formula->obtenerTasasNegocios();
                    foreach ($tasas as $tasa) :
                        
                            $tasa->setActivo(0);
                            $formula->agregarTasa($tasa);
                        
                    endforeach;
                    for ($i = 1; $i < 2; $i++) :
                        if ($this->getRequest()->getParam("tasa_$i")) :
                            $sucursalesNegocios = $this->_sucursalDao->obtenerTodosNegocios();
                            foreach ($sucursalesNegocios as $sursalNegocio) {
                                $tasa = new Doctrine_Model_Entities_Tasa();
                                $tasa->setValor($this->getRequest()->getParam("tasa_$i"));
                                $tasa->setDesde($this->getRequest()->getParam("cuota_desde_$i"));
                                $tasa->setHasta($this->getRequest()->getParam("cuota_hasta_$i"));
                                $tasa->setFormula($formula);
                                $tasa->setFecha(new DateTime());
                                $tasa->setActivo(1);
                                $tasa->setSucursal($sursalNegocio);
                                $formula->agregarTasa($tasa);
                            }

                        endif;
                    endfor;
                    if ($this->_formulaDao->guardar($formula)) :
                        $this->_flashMessenger->addMessage('Formula guardada correctamente.');
                    endif;
                elseif ($this->_hasParam("formula")) :
                    $formula_id = $this->getRequest()->getParam('formula');
                endif;
            }
        }

        $formula = $this->_formulaDao->obtenerPorId($formula_id);

        if ($formula) :
            $this->view->titulo = $formula->getDescripcion();
            $this->view->formula = $formula;
        endif;

        $this->view->messages = $this->_helper->flashMessenger->getCurrentMessages();
        $this->view->monto = $this->getRequest()->getParam('monto');
        $this->view->tasaUpdate = $this->getRequest()->getParam('tasaUpdateRequest');
        $this->view->formula_id = $formula_id;
        $this->view->formulas = $this->_formulaDao->obtenerTodosNegocios();
    }

}
